package com.lgs.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import com.lgs.base.ActivityManager;
import com.lgs.base.LocalInfoControlCenter;
import com.lgs.xlistviewtest.BuildConfig;

/**
 * Created by TomLeisen on 2018/7/9 下午3:27
 * Email: xy162162a@163.com
 * Description:
 */
public class MvpApplication extends Application {


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (isMainProcess()){//判断在APP进程才进行注册
            initSdk();
        }
    }

    private boolean isMainProcess() {
        try {
            android.app.ActivityManager am = (android.app.ActivityManager) getSystemService(ACTIVITY_SERVICE);//app运行状态管理权限
            if (am.getRunningAppProcesses() == null) {
                return true;
            }
            int pid = android.os.Process.myPid();//当前运行时的进程id
            String packageName = getPackageName();
            for (android.app.ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
                if (info.pid == pid && packageName.equals(info.processName)) {
                    return true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    private void initSdk() {
        picturesPermission();//初始化7.0系统拍照问题
        initEnvParams();
        initActivityLifeCb();// 初始化activity生命周期监听器

    }


    /**
     * 初始化7.0系统拍照问题
     */
    private void picturesPermission() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    }

    /**
     * 初始环境的配置
     */
    private void initEnvParams() {
        // 环境初始化
        LocalInfoControlCenter.INSTANCES.setLog_debug(BuildConfig.LOG_DEBUG);
        LocalInfoControlCenter.INSTANCES.setEnv(BuildConfig.ENVIRONMENT);
        LocalInfoControlCenter.INSTANCES.init(this);
    }

     /**
     * 初始化activity生命周期监听器
     */
    private void initActivityLifeCb() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                ActivityManager.INSTANCE.setCurrentActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                ActivityManager.INSTANCE.setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }


}
